﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Internet_Cafe.App_Code.DatabaseModel;
using System.Data.Objects;

namespace Internet_Cafe.App_Code.DataBaseAccess
{
    public abstract class DbAccountingAccess : DbAccess
    {
        /// <summary>
        /// Listanje na bills
        /// </summary>
        /// <returns></returns>
        public static List<Internet_Cafe.App_Code.DatabaseModel.Bill> GetBills()
        {
            using (computerCaffeDBEntities db = new computerCaffeDBEntities())
            {
                List<Internet_Cafe.App_Code.DatabaseModel.Bill> billList = new List<Internet_Cafe.App_Code.DatabaseModel.Bill>();
                var obj = (from p in db.Bill select p);
                foreach (Internet_Cafe.App_Code.DatabaseModel.Bill u in obj)
                    billList.Add(u);
                return billList;
            }
        }
        public static List<Internet_Cafe.App_Code.DatabaseModel.Bill> GetBills(string workerID)
        {
            using (computerCaffeDBEntities db = new computerCaffeDBEntities())
            {
                List<Internet_Cafe.App_Code.DatabaseModel.Bill> billList = new List<Internet_Cafe.App_Code.DatabaseModel.Bill>();
                var obj = (from bills in db.Bill where bills.WorkerID == workerID select bills);
                foreach (Internet_Cafe.App_Code.DatabaseModel.Bill u in obj)
                    billList.Add(u);
                return billList;
            }
        }

        public static Internet_Cafe.App_Code.DatabaseModel.Bill GetBill(string billID)
        {
            using (computerCaffeDBEntities db = new computerCaffeDBEntities())
            {
                try
                {
                    return (Internet_Cafe.App_Code.DatabaseModel.Bill)((from p in db.Bill where p.ID == billID select p).First());
                }
                catch
                {
                }
                return null;
            }
        }








        public static void PayBill(string billID, double cost, DateTime activeEnd)
        {
            using (computerCaffeDBEntities db = new computerCaffeDBEntities())
            {
                try
                {
                    Internet_Cafe.App_Code.DatabaseModel.Bill bill = (Internet_Cafe.App_Code.DatabaseModel.Bill)((from p in db.Bill where p.ID == billID select p).First());
                    bill.Cost = cost;
                    bill.isPayed = true;
                    bill.ActiveEnd = activeEnd;
                    db.SaveChanges();
                }
                catch
                {
                }
            }
        }


        public static string Insert(Internet_Cafe.App_Code.DatabaseModel.Bill bill)
        {
            using (computerCaffeDBEntities db = new computerCaffeDBEntities())
            {
                //try
                //{
                    string newID = Guid.NewGuid().ToString();
                    bill.ID = newID;
                    db.AddToBill(bill);
                    db.SaveChanges();

                    return bill.ID;
                //}
                //catch
                //{
                //}
                //return null;
            }
        }


        public static bool Delete(string billID)
        {
            using (computerCaffeDBEntities db = new computerCaffeDBEntities())
            {
                try
                {
                    Internet_Cafe.App_Code.DatabaseModel.Bill bill = (Internet_Cafe.App_Code.DatabaseModel.Bill)((from p in db.Bill where p.ID == billID select p).First());
                    db.DeleteObject(bill);
                    db.SaveChanges();
                }
                catch
                {
                    return false;
                }
                return true;
            }
        }
    }
}
